<?php
/*
 * Created on 22.12.2005
 *
 */

class c_foreachHandler 
{

    function startElement (& $sender, $attrs)
    {
        if (empty($attrs['items']))
        {
            die ("Attribute items is missing in foreach tag\n");
        }
        $var = substr($attrs['var'], 1, strlen($attrs['var'])-2);
        if (!preg_match('/\w+/', $var))
            die ("Attribute var must not contain EL code\n");
        if (!empty($attrs['key']))
        {
            $key = substr($attrs['key'], 1, strlen($attrs['key'])-2);
            if (!preg_match('/\w+/', $key))
                die ("Attribute key must not contain EL code\n");
            $key_ = '$'.$key;
        }
        $var_ = '$'.$var;
        $sender->appendCode('if (is_array('.$attrs['items'].')) foreach ('.$attrs['items']." as ".(isset($key_)?$key_.' => ':'').$var_.
            ") {\n\$actionform->set('".$var."', ".$var_.");\n".(isset($key_)?"\$actionform->set('".$key."', ".$key_.");\n":''));
    }
    
    function endElement (& $sender)
    {
        $sender->appendCode("}\n");
    }

}

?>
